//
//  TaskDatabase.m
//  iTimeline
//
//  Created by Mobioneer HV 04 on 1/9/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "TaskDatabase.h"
#import "Data.h"
#import "AppDelegate.h"
@interface TaskDatabase()
{
    NSArray *fetchedObjects;
}
@end
@implementation TaskDatabase
- (void)dealloc
{
    fetchedObjects = nil;
    self.listTask = nil;
}
- (id)init
{
    self = [super init];
    if (self) {
        self.listTask = [[NSMutableArray alloc]init];
    }
    return self;
}
- (id)init:(int)month Year:(int)year
{
    self = [super init];
    if (self) {
        self.listTask = [[NSMutableArray alloc]init];
        self.month = month;
        self.year = year;
    }
    return self;
}
- (void)query
{
    NSError *error;
    BOOL dayTask[32];
    
    for (int i = 1; i <= 31; i++) {
        dayTask[i] = NO;
    }
    
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Data" inManagedObjectContext:delegate.managedObjectContext];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"1=1"];
    [fetchRequest setEntity:entity];
    fetchedObjects = [delegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    for(int i = 0; i < fetchedObjects.count; i++)
    {
        Data* data = fetchedObjects[i];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd"];
        NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"vi_VN"];
        [dateFormatter setLocale:usLocale];
        int dayBegin = [[dateFormatter stringFromDate:data.from] integerValue];
        [dateFormatter setLocale:usLocale];
        int dayEnd = [[dateFormatter stringFromDate:data.to] integerValue];
        
        [dateFormatter setDateFormat:@"MM"];
        [dateFormatter setLocale:usLocale];
        int monthBegin = [[dateFormatter stringFromDate:data.from] integerValue];
        [dateFormatter setLocale:usLocale];
        int monthEnd = [[dateFormatter stringFromDate:data.to] integerValue];
        
        [dateFormatter setDateFormat:@"YYYY"];
        [dateFormatter setLocale:usLocale];
        int yearBegin = [[dateFormatter stringFromDate:data.from] integerValue];
        [dateFormatter setLocale:usLocale];
        int yearEnd = [[dateFormatter stringFromDate:data.to] integerValue];
        
        if (yearBegin < self.year)
        {
            if (yearEnd > self.year) {
                for (int j = 1; j <= 31;  j++)
                    dayTask[j] = YES;
            }
            if (yearEnd == self.year) {
                if (monthEnd > self.month)
                    for (int j = 1; j <= 31;  j++)
                        dayTask[j] = YES;
                if( monthEnd == self.month)
                    for (int j = 1; j <= dayEnd;  j++)
                        dayTask[j] = YES;
            }
            
        }
        if ( (yearBegin == self.year) && (monthBegin == self.month)) {
            if (monthEnd == self.month)
                for (int j = dayBegin; j <= dayEnd;  j++)
                    dayTask[j] = YES;
            else
                for (int j = dayBegin; j <= 31;  j++)
                    dayTask[j] = YES;
        }
    }
    //NSLog(@"Month = %d, Year = %d", self.month, self.year);
    for( int i = 1;i < 32; i++)
    {
        
        if(dayTask[i] == YES){
            [self.listTask addObject:[NSString stringWithFormat:@"%d", i]];
            //NSLog(@"index: %d",i);
        }
        else
            [self.listTask addObject:@"0"];
    }
}
@end